function data = LoadData(filename,path,filesize)
%
% LoadData: A function that converts CF card data into a column vector
%
% Usage: 
% data = LoadData(filename, filesize);
%
% Parameters:
% 1) filename -- Filename with complete filename
% 2) filesize -- Size of data file in kB
%
% Example:
% filename = test.bin, filename = 'C:\database', filesize = 80 kB
% To load this file into a data vector, type the following:
%   data = LoadData('C:\database\test.bin',80);
%
% You may then plot the data vector to view your recorded signal.
%
if(nargin < 3)
    filesize=Inf;
end
if(nargin<2)
    path=cd;
end
   
	%% Parameters
	BYTES_PER_KB = 1024;
	BYTES_PER_SAMPLE = 2;
    current_dir=cd;
    


	%% STEP 1: Open data file
    cd(path);
	fid = fopen(filename,'r','b');

	%% STEP 3: Read data from file, in the form of unsigned 8-bit values	
	byteStream = fread(fid,BYTES_PER_KB*filesize,'uchar');

	%% STEP 2: Allocate memory for the "data" variable
	numSamples = floor(length(byteStream)/2);
	
	%% STEP 4: Convert every two bytes into one 16-bit data sample
	k=1:numSamples;
	    data=uint16(byteStream(2*k-1)) + 256*uint16(byteStream(2*k));
	data=uint16(data);

	%% STEP 5: Close data file
	fclose(fid);
    cd(current_dir);
return
